<?php
namespace Act\Os;
use Lib\ErrorMessage;
use Lib\Common;
class GroupRole extends Backend{
	public function doGet(){
		$group_id = get('group_id',1,'absint');
		$group_role_mod = model('#GroupRole','adm_');
		$user_group_mod = model('#UserGroup','adm_');
		$group_info = $user_group_mod->getGroupById ( $group_id );
		if(!$group_info){
			$group_id =1;
			$group_info = $user_group_mod->getGroupById ( $group_id );
		}
		$group_role = $group_info ['group_role'];
		$group_role_array = explode ( ',', $group_role );

		#if post ...

		$this->display('index.php',array(
			'role_list' => $group_role_mod->getGroupRoles ( $group_id ),
			'group_id' => $group_id,
			'group_option_list' => $group_role_mod->getGroupForOptions (),
			'group_role' => $group_role_array,
		));
	}

	public function doPost(){
		$group_id = get('group_id',1,'absint');
		$menu_ids = post('menu_ids',array());
		$group_role_mod = model('#GroupRole','adm_');
		$user_group_mod = model('#UserGroup','adm_');
		$group_option_list = $group_role_mod->getGroupForOptions ();
		$group_info = $user_group_mod->getGroupById ( $group_id );
		if(!$group_info){
			$group_id =1;
			$group_info = $user_group_mod->getGroupById ( $group_id );
		}

		$group_role = $group_info ['group_role'];
		$group_role_array = explode ( ',', $group_role );

		if($group_id==1){
			$temp = array();
			foreach ($group_role_array as $group_role){
				
				//系统预留菜单id为100以内
				if($group_role>100){
					$temp[]=$group_role;
				}
			}
			
			$admin_role = array_diff($group_role_array,$temp);
			
			$menu_ids = array_merge($admin_role,$menu_ids);
			$menu_ids = array_unique($menu_ids);
			asort($menu_ids);
		}
		$group_role = join ( ',', $menu_ids );
		$group_data = array ('group_role' => $group_role );
		$result = $user_group_mod->updateGroupInfo ( $group_id, $group_data );
		if ($result>=0) {
			$this->sysLog(session('user_info')['user_name'], 'MODIFY', 'UserGroup' ,$group_id, json_encode($group_data) );
			if(model('#User','adm_')->reloadSession())
				return $this->success (ErrorMessage::SUCCESS_NEED_LOGIN,'os/groupRole');
		}else{
			$this->error("error",'os/groupRole');
		}	
	}
}